unit Modl_HTTP;

interface

uses
  SysUtils, Classes, rtcDataSrv, rtcInfo, rtcConn, rtcHttpSrv, DB,
  DBAccess, Uni, UniProvider, SQLServerUniProvider,JsonAndDB,
  SQLiteUniProvider,UniConnct,UniConfig,Class_GZJG,UniEngine,Class_Staf,
  Class_Dict,Class_WAGE_FL,Class_MOBL_GZJL,Class_MOBL_FFJL,Class_GZJG_FL,
  Class_MOBL_STAF;

type
  TModlHHTP = class(TDataModule)
    HtpSrvr1: TRtcHttpServer;
    RtcDataProvider1: TRtcDataProvider;
    SQLServerUniProvider1: TSQLServerUniProvider;
    SQLiteUniProvider1: TSQLiteUniProvider;
    procedure RtcDataProvider1CheckRequest(Sender: TRtcConnection);
    procedure RtcDataProvider1DataReceived(Sender: TRtcConnection);
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
  public
    procedure Start;
    procedure Stop;
  public
    procedure OnUniConnctCustomConnectionEventByOBJ(AConnectionMark:string;var OBJA:TUniConfig);
    procedure OnUniConnctCustomConnectionEventBySQL(AConnectionMark:string;var SQLA:string);
  end;

var
  ModlHHTP: TModlHHTP;

implementation
uses
  Class_KzDebug,Class_AppUtil,ActiveX,Class_KzUtils;

{$R *.dfm}

{ TModlHHTP }

procedure TModlHHTP.Start;
begin
  if not HtpSrvr1.isListening  then
  begin
    HtpSrvr1.ServerPort:='8186';
    HtpSrvr1.Listen();
    KzDebug.FileFmt('start listen...at:%S:%S',[HtpSrvr1.ServerAddr,HtpSrvr1.ServerPort]);
  end;
end;

procedure TModlHHTP.Stop;
begin
  if HtpSrvr1.isListening then
  begin
    HtpSrvr1.StopListen;
    KzDebug.FileLog('stop listen...');
  end;
end;

procedure TModlHHTP.RtcDataProvider1CheckRequest(Sender: TRtcConnection);
begin
  with TRtcDataServer(Sender) do
  begin
    {KzDebug.FileLog(Request.FileName);
    KzDebug.FileLog(Request.URL);
    KzDebug.FileFmt('params:%S',[Request.Params.Text]);
    KzDebug.FileFmt('1.STAFCARD:%S',[Request.Query.Value['STAFCARD']]);
    KzDebug.FileFmt('1.STAFPSWD:%S',[Request.Query.Value['STAFPSWD']]);}
    if UpperCase(Request.FileName)='/GETSIGN' then
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/GETINFO' then
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/GETSTAF' then 
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/GETGZJG' then 
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/GETDICT' then
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/GETGZJL' then 
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/GETDATA' then 
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/GETLIST' then 
    begin
      Accept;
    end;
  end;
end;

procedure TModlHHTP.RtcDataProvider1DataReceived(Sender: TRtcConnection);
var
  SQLA:string;
  StafCard:string;
  StafPswd:string;

  StafIdex:string;
  UnitLink:string;
  GZJGIDEX:string;
  HoldType:string;
  WAGEKJND:string;
  WAGEKJQJ:string;
  WAGEIDEX:string;
  IsDetail:string;

  UniConnct:TUniConnection;
  UniDatSet:TUniQuery;
  PListData:TStringList;
  QListData:TStringList;
  GListJGFL:TStringList;
begin
  CoInitialize(nil);
  PListData:=nil;
  QListData:=nil;
  GListJGFL:=nil;
  
  with TRtcDataServer(Sender) do
  begin
    if Request.Complete then
    begin
      if UpperCase(Request.FileName)='/GETINFO' then
      begin
        Write('[serveraddr]:[serverport]/getsign?stafcard=%s&stafpswd=%s'+#13);
        Write('[serveraddr]:[serverport]/getstaf?stafcard=33020319691117214X'+#13);
        Write('[serveraddr]:[serverport]/getgzjg?stafidex=%s'+#13);
        Write('[serveraddr]:[serverport]/getdict'+#13);
        Write('[serveraddr]:[serverport]/getgzjl?stafidex=%s&unitlink=%s&gzjgidex=%s&holdtype=%s&wagekind=%s&wagekjqj=%s&wageidex=%s');
      end else
      if UpperCase(Request.FileName)='/GETSIGN' then
      begin
        StafCard:=Request.Query.Value['STAFCARD'];
        StafPswd:=Request.Query.Value['STAFPSWD'];

        SQLA:='SELECT A.*,B.STAF_CARD FROM TBL_STAF_CNFG A,TBL_STAF B WHERE A.STAF_IDEX=B.STAF_IDEX AND A.UNIT_LINK=B.UNIT_LINK AND B.STAF_CARD=%S AND STAF_PSWD=%S';
        SQLA:=Format(SQLA,[QuotedStr(StafCard),QuotedStr(StafPswd)]);
        try
          UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
          //-<
          UniDatSet:=UniConnctEx.GetDataSet(SQLA,UniConnct);
          if UniDatSet.RecordCount>=1 then
          begin
            Write('login:true');
          end else
          begin
            Write('login:false');
          end;
          //->
        finally
          FreeAndNil(UniConnct);
          FreeAndNil(UniDatSet);
        end;
        
      end else
      if UpperCase(Request.FileName)='/GETSTAF' then
      begin
        StafCard:=Request.Query.Value['STAFCARD'];

        SQLA:='SELECT TOP 1 * FROM TBL_STAF WHERE STAF_CARD=%S ORDER BY STAF_BDID DESC';
        SQLA:=Format(SQLA,[QuotedStr(StafCard)]);
        
        try
          UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
          //-<
          if PListData=nil then
          begin
            PListData:=TStringList.Create;
          end;
          TKzUtils.JustCleanList(PListData);
          TStaf.ListDB(SQLA,UniConnct,PListData);

          if PListData.Count>0 then
          begin
            Write(TStaf.TOJSON(PListData));
          end else
          begin
            Write('getstaf:false');
          end;
          //->
        finally
          FreeAndNil(UniConnct);
          if PListData<>nil then TKzUtils.TryFreeAndNil(PListData);
        end;

      end else
      if UpperCase(Request.FileName)='/GETGZJG' then
      begin
        StafIdex:=Request.Query.Value['STAFIDEX'];

        SQLA:=TGZJG.ExpSQL(StafIdex);

        try
          UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
          //-<
          if PListData=nil then
          begin
            PListData:=TStringList.Create;
          end;
          TKzUtils.JustCleanList(PListData);

          TGZJG.ListDB(SQLA,UniConnct,PListData);
          if PListData.Count>0 then
          begin
            TGZJG.CopyIt(PListData);
            Write(TGZJG.TOJSON(PListData));
          end else
          begin
            Write('getgzjg:false');
          end;
          //->
        finally
          FreeAndNil(UniConnct);
          if PListData<>nil then TKzUtils.TryFreeAndNil(PListData);
        end;       

      end else
      if UpperCase(Request.FileName)='/GETDICT' then
      begin
        SQLA:='SELECT * FROM TBL_DICT WHERE DICT_MODE IN (%S,%S)';
        SQLA:=Format(SQLA,[QuotedStr('00008'),QuotedStr('00009')]);
        
        try
          UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
          //-<
          if PListData=nil then
          begin
            PListData:=TStringList.Create;
          end;
          TKzUtils.JustCleanList(PListData);

          TDict.ListDB(SQLA,UniConnct,PListData);
          if PListData.Count>0 then
          begin
            Write(TDict.TOJSON(PListData,otUpperCase));
          end else
          begin
            Write('getdict:false');
          end;
          //->
        finally
          FreeAndNil(UniConnct);
          if PListData<>nil then TKzUtils.TryFreeAndNil(PListData);
        end;
      end else
      if UpperCase(Request.FileName)='/GETGZJL' then 
      begin
        IsDetail:='0';
        StafIdex:=Request.Query.Value['STAFIDEX'];
        UnitLink:=Request.Query.Value['UNITLINK'];
        GZJGIDEX:=Request.Query.Value['GZJGIDEX'];
        HoldType:=Request.Query.Value['HOLDTYPE'];
        WAGEKJND:=Request.Query.Value['WAGEKJND'];
        WAGEKJQJ:=Request.Query.Value['WAGEKJQJ'];
        WAGEIDEX:=Request.Query.Value['WAGEIDEX'];
        IsDetail:=Request.Query.Value['ISDETAIL'];

        KzDebug.FileFmt('%S,%S,%S,%S,%S,%S,%S',[STAFIDEX,UnitLink,GZJGIDEX,HoldType,WAGEKJND,WAGEKJQJ,WAGEIDEX]);

        if IsDetail='1' then  //get gzjl directly.
        begin
          SQLA:=TMOBLGZJL.ExpSQL(StafIdex,UnitLink,GZJGIDEX,WAGEKJND,WAGEKJQJ,WAGEIDEX,HoldType);

          try
            UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
            //-<
            if PListData=nil then
            begin
              PListData:=TStringList.Create;
            end;
            TKzUtils.JustCleanList(PListData);
            TWageFl.ListDB(SQLA,UniConnct,PListData);

            if GListJGFL=nil then
            begin
              GListJGFL:=TStringList.Create;
            end;
            TKzUtils.JustCleanList(GListJGFL);
            SQLA:='SELECT * FROM TBL_GZJG_FL WHERE UNIT_LINK=%S AND GZJG_IDEX=%S ORDER BY GZJG_FLBH';
            SQLA:=Format(SQLA,[QuotedStr(UnitLink),GZJGIDEX]);
            TGZJGFL.ListDB(SQLA,UniConnct,GListJGFL);

            if QListData=nil then
            begin
              QListData:=TStringList.Create;
            end;
            TKzUtils.JustCleanList(QListData);

            //YXC_2014_05_10_09_32_53_<
            TWageFl.CopyIt(UnitLink,GZJGIDEX,PListData,GListJGFL,QListData,nil);
            //YXC_2014_05_10_09_32_53_>

            if QListData.Count>0 then
            begin
              Write(TMOBLGZJL.TOJSON(QListData));
            end else
            begin
              Write('getgzjl:false');
            end;
            //->
          finally
            FreeAndNil(UniConnct);
            if PListData<>nil then TKzUtils.TryFreeAndNil(PListData);
            if QListData<>nil then TKzUtils.TryFreeAndNil(QListData);
            if GListJGFL<>nil then TKzUtils.TryFreeAndNil(GListJGFL);
          end;
        end else
        begin                 //get ffjl 
          SQLA:=TMOBLFFJL.ExpSQL(StafIdex,UnitLink,GZJGIDEX,WAGEKJND,WAGEKJQJ,WAGEIDEX,HoldType);

          try
            UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
            //-<
            if PListData=nil then
            begin
              PListData:=TStringList.Create;
            end;
            TKzUtils.JustCleanList(PListData);

            TMOBLFFJL.ListDB(SQLA,UniConnct,PListData);
            if PListData.Count=0 then
            begin
              Write('getgzjl:false');
            end else
            if PListData.Count=1 then   //get gzjl
            begin
              //copy it from above
              SQLA:=TMOBLGZJL.ExpSQL(StafIdex,UnitLink,GZJGIDEX,WAGEKJND,WAGEKJQJ,WAGEIDEX,HoldType);

              try
                //UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
                //-<
                if PListData=nil then
                begin
                  PListData:=TStringList.Create;
                end;
                TKzUtils.JustCleanList(PListData);
                TWageFl.ListDB(SQLA,UniConnct,PListData);

                if GListJGFL=nil then
                begin
                  GListJGFL:=TStringList.Create;
                end;
                TKzUtils.JustCleanList(GListJGFL);
                SQLA:='SELECT * FROM TBL_GZJG_FL WHERE UNIT_LINK=%S AND GZJG_IDEX=%S ORDER BY GZJG_FLBH';
                SQLA:=Format(SQLA,[QuotedStr(UnitLink),GZJGIDEX]);
                TGZJGFL.ListDB(SQLA,UniConnct,GListJGFL);

                if QListData=nil then
                begin
                  QListData:=TStringList.Create;
                end;
                TKzUtils.JustCleanList(QListData);

                //YXC_2014_05_10_09_32_53_<
                TWageFl.CopyIt(UnitLink,GZJGIDEX,PListData,GListJGFL,QListData,nil);
                //YXC_2014_05_10_09_32_53_>

                if QListData.Count>0 then
                begin
                  Write(TMOBLGZJL.TOJSON(QListData));
                end else
                begin
                  Write('getgzjl:false');
                end;
              finally
                //FreeAndNil(UniConnct);
                if PListData<>nil then TKzUtils.TryFreeAndNil(PListData);
                if QListData<>nil then TKzUtils.TryFreeAndNil(QListData);
                if GListJGFL<>nil then TKzUtils.TryFreeAndNil(GListJGFL);
                PListData:=nil;
                QListData:=nil;
                GListJGFL:=nil;
              end;
            end else
            begin
              Write(TMOBLFFJL.TOJSON(PListData));
            end;
            //->
          finally
            FreeAndNil(UniConnct);
            if PListData<>nil then TKzUtils.TryFreeAndNil(PListData);
          end;        
        end;
      end else
      if UpperCase(Request.FileName)='/GETLIST' then
      begin
        StafIdex:=Request.Query.Value['STAFIDEX'];
        SQLA    :=TMOBLSTAF.ExpSQL(StafIdex);

        try
          UniConnct:=UniConnctEx.GetConnection(CONST_MARK_XZGL);
          //-<
          if PListData=nil then
          begin
            PListData:=TStringList.Create;
          end;
          TMOBLSTAF.ListDB(SQLA,UniConnct,PListData);
          Write(TMOBLSTAF.TOJSON(PListData));
          //->
        finally
          FreeAndNil(UniConnct);
          if PListData<>nil then TKzUtils.TryFreeAndNil(PListData);
        end;
      end;
    end;
  end;

  CoUninitialize;
end;

procedure TModlHHTP.DataModuleCreate(Sender: TObject);
begin
  UniConnctEx.ConnctMode:=cmBySQL;
  UniConnctEx.OnUniConnctCustomConnectionEventBySQL:=OnUniConnctCustomConnectionEventBySQL;

  CoInitialize(nil);
end;

procedure TModlHHTP.OnUniConnctCustomConnectionEventByOBJ(
  AConnectionMark: string; var OBJA: TUniConfig);
begin

end;

procedure TModlHHTP.OnUniConnctCustomConnectionEventBySQL(
  AConnectionMark: string; var SQLA: string);
begin
  SQLA:='SELECT * FROM TBL_UNICONFIG WHERE UNIX_MARK=%S AND UNIX_STAT=1';
  SQLA:=Format(SQLA,[QuotedStr(AConnectionMark)]);  
end;

procedure TModlHHTP.DataModuleDestroy(Sender: TObject);
begin
  CoUninitialize;
end;

end.



